<?php
class GroupeController extends Controller {
	protected $group;
	private $topSalle;
	private $topMessage;
	
	public function __construct() {
		$this->topSalle = Loader::loadModel('Salle');
		$this->topSalle = Salle::getTopSalles();
		$this->topMessage = Loader::loadModel('Message');
		$this->topMessage = Message::get10MostLikedMessages();
	}
	
	public function view($page='restrictedPage',$params=null) {
		$this->group = Loader::loadModel('Groupe');

		$userController = Loader::loadController('user');
		if($userController->isConnected()) {
			$session_user = $userController->getUserSessionInfos();
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$allUsers = User::getAllUsers($userController->getUserSessionInfos()->getId());
			$menu = 'topUserLogged';
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$menu = 'topUserLogged';
			else
				$menu = 'topAdmin';
			
			$postAdd = ParameterManager::getInstance()->getPost('ADDUSER');
			if(isset($postAdd)) {
				$idgroupe = ParameterManager::getInstance()->getPost('IDGROUPE');
				$usersToAdd = ParameterManager::getInstance()->getPost('USERS');
				if(!is_array($usersToAdd))
					$usersToAdd = array();
				
				$thegroup = Loader::loadModel('Groupe');
				$thegroup = Groupe::getGroupe($idgroupe);
				foreach($usersToAdd as $userId)
					$thegroup->ajouterMembre($userId);
			}
			
			$postDelete = ParameterManager::getInstance()->getPost('DELETE');
			if(isset($postDelete)) {
				$idgroupe = ParameterManager::getInstance()->getPost('IDGROUPE');
				$userToDelete = ParameterManager::getInstance()->getPost('IDUSER');
				$thegroup = Loader::loadModel('Groupe');
				$thegroup = Groupe::getGroupe($idgroupe);
				$thegroup->supprimerMembres($userToDelete);
			}
			
			
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$this->group = Groupe::getGroupesOfUser($session_user->getId());
			else
				$this->group = Groupe::getAllGroupes();
			$view = Loader::loadView('View', array('top'=>$menu,'left'=>'leftColumn','center'=>'groupes'));
			$view->setParam('groupes',$this->group);
			$view->setParam('user',$session_user);
			$view->setParam('allUsers',$allUsers);
			
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			$view->render();
		} else {
				$view = Loader::loadView('View', array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
				$view->setParam('topsalles',$this->topSalle);
				$view->setParam('topmessages',$this->topMessage);
				$view->render();
		}
	}
	
	public function create() {
		$userController = Loader::loadController('user');
		if($userController->isConnected()) {
			$session_user = $userController->getUserSessionInfos();
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$menu = 'topUserLogged';
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$menu = 'topUserLogged';
			else
				$menu = 'topAdmin';
			
			$isPost = ParameterManager::getInstance()->getPost('POSTGROUPE');
			if(isset($isPost)) {
				$nomGroupe = ParameterManager::getInstance()->getPost('NOMGROUPE');
				$membres = ParameterManager::getInstance()->getPost('USERS');
				Loader::loadModel('Groupe');
				Groupe::createGroupe($userController->getUserSessionInfos()->getId(), $nomGroupe, $membres);
			}
			
			$allUsers = User::getAllUsers($userController->getUserSessionInfos()->getId());
			if(isset($isPost))
				$view = Loader::loadView('View', 
										array('title'=>'Groupe créé','top'=>$menu,'left'=>'leftColumn','center'=>'groupeCreated'));
			else
				$view = Loader::loadView('View', 
										array('title'=>'Créer un groupe','top'=>$menu,'left'=>'leftColumn','center'=>'createGroup'));
			$view->setParam('groupes',$this->group);
			$view->setParam('user',$session_user);
			$view->setParam('allUsers', $allUsers);
			
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			$view->render();
		} else {
				$view = Loader::loadView('View', array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
				$view->setParam('topsalles',$this->topSalle);
				$view->setParam('topmessages',$this->topMessage);
				$view->render();
		}
	}
	
	public function defaut() {
		$this->view();
	}
	
}